home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / shlex.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  7.5 KB  |  304 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''A lexical analyzer class for simple shell-like syntaxes.'''
  5. import os.path as os
  6. import sys
  7. from collections import deque
  8.  
  9. try:
  10.     from cStringIO import StringIO
  11. except ImportError:
  12.     from StringIO import StringIO
  13.  
  14. __all__ = [
  15.     'shlex',
  16.     'split']
  17.  
  18. class shlex:
  19.     '''A lexical analyzer class for simple shell-like syntaxes.'''
  20.     
  21.     def __init__(self, instream = None, infile = None, posix = False):
  22.         if isinstance(instream, basestring):
  23.             instream = StringIO(instream)
  24.         
  25.         if instream is not None:
  26.             self.instream = instream
  27.             self.infile = infile
  28.         else:
  29.             self.instream = sys.stdin
  30.             self.infile = None
  31.         self.posix = posix
  32.         if posix:
  33.             self.eof = None
  34.         else:
  35.             self.eof = ''
  36.         self.commenters = '#'
  37.         self.wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  38.         if self.posix:
  39.             self.wordchars += '\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde'
  40.         
  41.         self.whitespace = ' \t\r\n'
  42.         self.whitespace_split = False
  43.         self.quotes = '\'"'
  44.         self.escape = '\\'
  45.         self.escapedquotes = '"'
  46.         self.state = ' '
  47.         self.pushback = deque()
  48.         self.lineno = 1
  49.         self.debug = 0
  50.         self.token = ''
  51.         self.filestack = deque()
  52.         self.source = None
  53.         if self.debug:
  54.             print 'shlex: reading from %s, line %d' % (self.instream, self.lineno)
  55.         
  56.  
  57.     
  58.     def push_token(self, tok):
  59.         '''Push a token onto the stack popped by the get_token method'''
  60.         if self.debug >= 1:
  61.             print 'shlex: pushing token ' + repr(tok)
  62.         
  63.         self.pushback.appendleft(tok)
  64.  
  65.     
  66.     def push_source(self, newstream, newfile = None):
  67.         """Push an input source onto the lexer's input source stack."""
  68.         if isinstance(newstream, basestring):
  69.             newstream = StringIO(newstream)
  70.         
  71.         self.filestack.appendleft((self.infile, self.instream, self.lineno))
  72.         self.infile = newfile
  73.         self.instream = newstream
  74.         self.lineno = 1
  75.         if self.debug:
  76.             if newfile is not None:
  77.                 print 'shlex: pushing to file %s' % (self.infile,)
  78.             else:
  79.                 print 'shlex: pushing to stream %s' % (self.instream,)
  80.         
  81.  
  82.     
  83.     def pop_source(self):
  84.         '''Pop the input source stack.'''
  85.         self.instream.close()
  86.         (self.infile, self.instream, self.lineno) = self.filestack.popleft()
  87.         if self.debug:
  88.             print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
  89.         
  90.         self.state = ' '
  91.  
  92.     
  93.     def get_token(self):
  94.         """Get a token from the input stream (or from stack if it's nonempty)"""
  95.         if self.pushback:
  96.             tok = self.pushback.popleft()
  97.             if self.debug >= 1:
  98.                 print 'shlex: popping token ' + repr(tok)
  99.             
  100.             return tok
  101.         raw = self.read_token()
  102.         while raw == self.eof:
  103.             if not self.filestack:
  104.                 return self.eof
  105.             self.pop_source()
  106.             raw = self.get_token()
  107.             continue
  108.             self.filestack
  109.         return raw
  110.  
  111.     
  112.     def read_token(self):
  113.         quoted = False
  114.         escapedstate = ' '
  115.         while True:
  116.             nextchar = self.instream.read(1)
  117.             if nextchar == '\n':
  118.                 self.lineno = self.lineno + 1
  119.             
  120.             if self.debug >= 3:
  121.                 print 'shlex: in state', repr(self.state), 'I see character:', repr(nextchar)
  122.             
  123.             if self.state is None:
  124.                 self.token = ''
  125.                 break
  126.                 continue
  127.             if self.state == ' ':
  128.                 if not nextchar:
  129.                     self.state = None
  130.                     break
  131.                 elif nextchar in self.whitespace:
  132.                     if self.debug >= 2:
  133.                         print 'shlex: I see whitespace in whitespace state'
  134.                     
  135.                     if (self.token or self.posix) and quoted:
  136.                         break
  137.                     
  138.                 elif nextchar in self.commenters:
  139.                     self.instream.readline()
  140.                     self.lineno = self.lineno + 1
  141.                 elif self.posix and nextchar in self.escape:
  142.                     escapedstate = 'a'
  143.                     self.state = nextchar
  144.                 elif nextchar in self.wordchars:
  145.                     self.token = nextchar
  146.                     self.state = 'a'
  147.                 elif nextchar in self.quotes:
  148.                     if not self.posix:
  149.                         self.token = nextchar
  150.                     
  151.                     self.state = nextchar
  152.                 elif self.whitespace_split:
  153.                     self.token = nextchar
  154.                     self.state = 'a'
  155.                 else:
  156.                     self.token = nextchar
  157.                     if (self.token or self.posix) and quoted:
  158.                         break
  159.                     
  160.             nextchar in self.escape
  161.             if self.state in self.quotes:
  162.                 quoted = True
  163.                 if not nextchar:
  164.                     if self.debug >= 2:
  165.                         print 'shlex: I see EOF in quotes state'
  166.                     
  167.                     raise ValueError, 'No closing quotation'
  168.                 nextchar
  169.                 if nextchar == self.state:
  170.                     if not self.posix:
  171.                         self.token = self.token + nextchar
  172.                         self.state = ' '
  173.                         break
  174.                     else:
  175.                         self.state = 'a'
  176.                 elif self.posix and nextchar in self.escape and self.state in self.escapedquotes:
  177.                     escapedstate = self.state
  178.                     self.state = nextchar
  179.                 else:
  180.                     self.token = self.token + nextchar
  181.             self.state in self.escapedquotes
  182.             if self.state in self.escape:
  183.                 if not nextchar:
  184.                     if self.debug >= 2:
  185.                         print 'shlex: I see EOF in escape state'
  186.                     
  187.                     raise ValueError, 'No escaped character'
  188.                 nextchar
  189.                 if escapedstate in self.quotes and nextchar != self.state and nextchar != escapedstate:
  190.                     self.token = self.token + self.state
  191.                 
  192.                 self.token = self.token + nextchar
  193.                 self.state = escapedstate
  194.                 continue
  195.             if self.state == 'a':
  196.                 if not nextchar:
  197.                     self.state = None
  198.                     break
  199.                 elif nextchar in self.whitespace:
  200.                     if self.debug >= 2:
  201.                         print 'shlex: I see whitespace in word state'
  202.                     
  203.                     self.state = ' '
  204.                     if (self.token or self.posix) and quoted:
  205.                         break
  206.                     
  207.                 elif nextchar in self.commenters:
  208.                     self.instream.readline()
  209.                     self.lineno = self.lineno + 1
  210.                     if self.posix:
  211.                         self.state = ' '
  212.                         if (self.token or self.posix) and quoted:
  213.                             break
  214.                         
  215.                     
  216.                 elif self.posix and nextchar in self.quotes:
  217.                     self.state = nextchar
  218.                 elif self.posix and nextchar in self.escape:
  219.                     escapedstate = 'a'
  220.                     self.state = nextchar
  221.                 elif nextchar in self.wordchars and nextchar in self.quotes or self.whitespace_split:
  222.                     self.token = self.token + nextchar
  223.                 else:
  224.                     self.pushback.appendleft(nextchar)
  225.                     if self.debug >= 2:
  226.                         print 'shlex: I see punctuation in word state'
  227.                     
  228.                     self.state = ' '
  229.                     if self.token:
  230.                         break
  231.                     
  232.             self.whitespace_split
  233.         result = self.token
  234.         self.token = ''
  235.         if self.posix and not quoted and result == '':
  236.             result = None
  237.         
  238.         if self.debug > 1:
  239.             if result:
  240.                 print 'shlex: raw token=' + repr(result)
  241.             else:
  242.                 print 'shlex: raw token=EOF'
  243.         
  244.         return result
  245.  
  246.     
  247.     def sourcehook(self, newfile):
  248.         '''Hook called on a filename to be sourced.'''
  249.         if newfile[0] == '"':
  250.             newfile = newfile[1:-1]
  251.         
  252.         if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
  253.             newfile = os.path.join(os.path.dirname(self.infile), newfile)
  254.         
  255.         return (newfile, open(newfile, 'r'))
  256.  
  257.     
  258.     def error_leader(self, infile = None, lineno = None):
  259.         '''Emit a C-compiler-like, Emacs-friendly error-message leader.'''
  260.         if infile is None:
  261.             infile = self.infile
  262.         
  263.         if lineno is None:
  264.             lineno = self.lineno
  265.         
  266.         return '"%s", line %d: ' % (infile, lineno)
  267.  
  268.     
  269.     def __iter__(self):
  270.         return self
  271.  
  272.     
  273.     def next(self):
  274.         token = self.get_token()
  275.         if token == self.eof:
  276.             raise StopIteration
  277.         token == self.eof
  278.         return token
  279.  
  280.  
  281.  
  282. def split(s, comments = False, posix = True):
  283.     lex = shlex(s, posix = posix)
  284.     lex.whitespace_split = True
  285.     if not comments:
  286.         lex.commenters = ''
  287.     
  288.     return list(lex)
  289.  
  290. if __name__ == '__main__':
  291.     if len(sys.argv) == 1:
  292.         lexer = shlex()
  293.     else:
  294.         file = sys.argv[1]
  295.         lexer = shlex(open(file), file)
  296.     while None:
  297.         tt = lexer.get_token()
  298.         if tt:
  299.             print 'Token: ' + repr(tt)
  300.             continue
  301.         break
  302.         continue
  303. __name__ == '__main__'
  304.